﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;
using YJKArchUtils.Utils;
using YJKArchUtils.Utils.Generics;

namespace YJKRevitParapet.Analyzer
{
	public class TopPoleAnalyzer
	{
		public TopPoleAnalyzer(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
		}

		public bool Analyse(List<Pole2D> poleList, ref List<Pole2D> analyzedPoleList)
		{
			bool result;
			try
			{
				if (poleList.Count == 0)
				{
					result = false;
				}
				else
				{
					List<Pole2D> poleList2 = this.SegmentPoleList(poleList);
					List<Pole2D> poleList3 = this.FilterOverlapPoleList(poleList2);
					List<Node2D> theNodes = new List<Node2D>();
					this.InitData(poleList3, Node2D.SearchType._Clockwise, ref theNodes);
					this.GetAllPoleList(theNodes, ref analyzedPoleList);
					this.MergeOverlapWalls(ref analyzedPoleList);
					result = true;
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public bool MergeEdge(List<Pole2D> poleList, ref List<Pole2D> analyzedPoleList)
		{
			bool result;
			try
			{
				if (poleList.Count == 0)
				{
					result = false;
				}
				else
				{
					List<Node2D> theNodes = new List<Node2D>();
					this.InitData(poleList, Node2D.SearchType._Clockwise, ref theNodes);
					this.MergeEdge(ref theNodes);
					this.GetAllPoleList(theNodes, ref analyzedPoleList);
					result = true;
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public void FormatDoubleLine(ref List<Pole2D> poleList)
		{
			try
			{
				this.FormatContinuousPoles(ref poleList);
			}
			catch (Exception)
			{
			}
		}

		private List<Pole2D> SegmentPoleList(List<Pole2D> poleList)
		{
			List<Pole2D> list = new List<Pole2D>();
			List<List<XYZ>> list2 = new List<List<XYZ>>();
			List<Curve> list3 = new List<Curve>();
			this.GetCurvesByPoles(poleList, ref list3, ref list2);
			this.Intersect(list3, ref list2);
			for (int i = 0; i < poleList.Count; i++)
			{
				Pole2D pole2D = poleList[i];
				Curve curve = list3[i];
				List<XYZ> intersectPoints = list2[i];
				List<XYZ> list4 = this.UniqueAndSortIntersectPoints(curve, intersectPoints);
				if (list4.Count >= 2)
				{
					if (list4.Count == 2)
					{
						list.Add(pole2D);
					}
					else
					{
						double positiveHalfWidth = pole2D.PositiveHalfWidth;
						double negativeHalfWidth = pole2D.NegativeHalfWidth;
						XYZ center = pole2D.Center;
						for (int j = 0; j < list4.Count - 1; j++)
						{
							XYZ xyz = list4[j];
							XYZ xyz2 = list4[j + 1];
							if (!Geometry.LessThan(xyz.DistanceTo(xyz2), 0.0001))
							{
								try
								{
									if (pole2D.IsArcPole)
									{
										Pole2D item = Pole2D.NewStructure2DPole(pole2D.ComponentId, xyz, xyz2, center, XYZ.BasisZ, pole2D.Elevation, positiveHalfWidth, negativeHalfWidth);
										list.Add(item);
									}
									else
									{
										Pole2D item2 = Pole2D.NewStructure2DPole(pole2D.ComponentId, xyz, xyz2, pole2D.Elevation, positiveHalfWidth, negativeHalfWidth);
										list.Add(item2);
									}
								}
								catch (Exception)
								{
								}
							}
						}
					}
				}
			}
			return list;
		}

		private void FormatContinuousPoles(ref List<Pole2D> continuousPoles)
		{
			List<Node2D> list = new List<Node2D>();
			this.InitData(continuousPoles, Node2D.SearchType._Anticlockwise, ref list);
			this.TrimAllNode(ref list);
		}

		private void TrimAllNode(ref List<Node2D> theNodes)
		{
			foreach (Node2D node2D in theNodes)
			{
				int count = node2D.PoleList.Count;
				if (count != 1)
				{
					node2D.AnticlockwiseSortElements();
					List<Pole2D> poleList = node2D.PoleList;
					for (int i = 0; i < count; i++)
					{
						int index = i;
						int index2 = i + 1;
						if (i == count - 1)
						{
							index2 = 0;
						}
						Pole2D pole2D = poleList[index];
						Pole2D pole2D2 = poleList[index2];
						double dSAngle = 0.0;
						double dEAngle = 0.0;
						pole2D.GetAngle(node2D.Position, ref dSAngle);
						pole2D2.GetAngle(node2D.Position, ref dEAngle);
						if (!Geometry.IsEqual(Geometry.BetweenTheAngles(dSAngle, dEAngle, true), Math.PI))
						{
							this.TrimPoleSideline(node2D.Position, ref pole2D, ref pole2D2);
						}
					}
				}
			}
		}

		private void TrimPoleSideline(XYZ position, ref Pole2D pole1, ref Pole2D pole2)
		{
			XYZ xyz = pole1.CurNegativeStartPoint(position);
			XYZ xyz2 = pole1.CurNegativeEndPoint(position);
			if (Geometry.IsEqual(xyz, xyz2))
			{
				return;
			}
			Curve curve;
			if (pole1.IsArcPole)
			{
				XYZ center = pole1.Center;
				XYZ xyz3 = Geometry.CalculatMidPoint(xyz, xyz2, center, XYZ.BasisZ);
				curve = Arc.Create(xyz, xyz2, xyz3);
			}
			else
			{
				curve = Line.CreateBound(xyz, xyz2);
			}
			XYZ xyz4 = pole2.CurPositiveStartPoint(position);
			XYZ xyz5 = pole2.CurPositiveEndPoint(position);
			if (Geometry.IsEqual(xyz4, xyz5))
			{
				return;
			}
			Curve curve2;
			if (pole2.IsArcPole)
			{
				XYZ center2 = pole2.Center;
				XYZ xyz6 = Geometry.CalculatMidPoint(xyz4, xyz5, center2, XYZ.BasisZ);
				curve2 = Arc.Create(xyz4, xyz5, xyz6);
			}
			else
			{
				curve2 = Line.CreateBound(xyz4, xyz5);
			}
			XYZ xyz7 = Geometry.IntersectWithTwoCurves(curve, curve2, position, true, true, this.m_Revit.Application.Application);
			if (xyz7 != null)
			{
				pole1.SetCurNegativeStartPoint(position, xyz7);
				pole2.SetCurPositiveStartPoint(position, xyz7);
			}
		}

		private List<XYZ> UniqueAndSortIntersectPoints(Curve curve, List<XYZ> intersectPoints)
		{
			Arc arc = curve as Arc;
			if (null != arc)
			{
				XYZ endPoint = arc.GetEndPoint(0);
				if (arc.Normal.Z < 0.0)
				{
					endPoint = arc.GetEndPoint(1);
				}
				intersectPoints.Sort(new YJKArchUtils.Utils.Generics.AngleComparer(endPoint, arc.Center));
			}
			Line line = curve as Line;
			if (null != line)
			{
				XYZ endPoint2 = line.GetEndPoint(0);
				intersectPoints.Sort(new DistanceComparer(endPoint2));
			}
			IEnumerable<XYZ> enumerable = intersectPoints.Distinct(new Comparint<XYZ>((XYZ x, XYZ y) => x != null && y != null && x.DistanceTo(y) < 0.001));
			List<XYZ> list = new List<XYZ>();
			foreach (XYZ item in enumerable)
			{
				list.Add(item);
			}
			return list;
		}

		private void GetCurvesByPoles(List<Pole2D> poleList, ref List<Curve> centerLineList, ref List<List<XYZ>> intersectPointList)
		{
			int i = 0;
			while (i < poleList.Count)
			{
				Pole2D pole2D = poleList[i];
				XYZ startPoint = pole2D.StartPoint;
				XYZ endPoint = pole2D.EndPoint;
				Curve item = null;
				try
				{
					if (pole2D.IsArcPole)
					{
						XYZ xyz = XYZ.BasisZ;
						if (Geometry.LessThan(pole2D.Bulge, 0.0))
						{
							xyz = -xyz;
						}
						XYZ center = pole2D.Center;
						XYZ radianPnt = Geometry.CalculatMidPoint(startPoint, endPoint, center, xyz);
						item = this.m_Revit.Application.Application.CreatYJKArc(startPoint, endPoint, radianPnt);
					}
					else
					{
						item = YJKLineEx.YJKGetBound(startPoint, endPoint);
					}
				}
				catch (Exception)
				{
					poleList.RemoveAt(i);
					i--;
					goto IL_CC;
				}
				goto IL_99;
				IL_CC:
				i++;
				continue;
				IL_99:
				List<XYZ> list = new List<XYZ>();
				list.Add(pole2D.StartPoint);
				list.Add(pole2D.EndPoint);
				intersectPointList.Add(list);
				centerLineList.Add(item);
				goto IL_CC;
			}
		}

		private void Intersect(List<Curve> centerLineList, ref List<List<XYZ>> intersectPointList)
		{
			for (int i = 0; i < centerLineList.Count; i++)
			{
				Curve curve = centerLineList[i];
				for (int j = i + 1; j < centerLineList.Count; j++)
				{
					Curve curve2 = centerLineList[j];
					try
					{
						IntersectionResultArray intersectionResultArray;
                        if ((int)curve.Intersect(curve2, out intersectionResultArray) == 8)
						{
							foreach (object obj in intersectionResultArray)
							{
								IntersectionResult intersectionResult = (IntersectionResult)obj;
								if (intersectionResult.XYZPoint != null)
								{
									intersectPointList[i].Add(intersectionResult.XYZPoint);
									intersectPointList[j].Add(intersectionResult.XYZPoint);
								}
							}
						}
					}
					catch (Exception)
					{
					}
				}
			}
		}

		private List<Pole2D> FilterOverlapPoleList(List<Pole2D> poleList)
		{
			List<Pole2D> list = new List<Pole2D>();
			list.AddRange(poleList);
			for (int i = 0; i < list.Count; i++)
			{
				for (int j = i + 1; j < list.Count; j++)
				{
					if (list[i].SameAs(list[j]))
					{
						if (Geometry.LessThan(list[i].Elevation, list[j].Elevation))
						{
							Pole2D value = list[i];
							list[i] = list[j];
							list[j] = value;
						}
						list.RemoveAt(j);
						j--;
					}
				}
			}
			return list;
		}

		private void InitData(List<Pole2D> poleList, Node2D.SearchType searchType, ref List<Node2D> theNodes)
		{
			Dictionary<XYZ, Node2D> dictionary = new Dictionary<XYZ, Node2D>(new TopPoleAnalyzer.PointComparer());
			foreach (Pole2D pole2D in poleList)
			{
				XYZ xyz = pole2D.StartPoint;
				if (!dictionary.ContainsKey(xyz))
				{
					Node2D value = new Node2D(xyz, new List<Pole2D>
					{
						pole2D
					});
					dictionary.Add(xyz, value);
				}
				else
				{
					dictionary[xyz].AddPole(pole2D, false);
				}
				xyz = pole2D.EndPoint;
				if (!dictionary.ContainsKey(xyz))
				{
					Node2D value2 = new Node2D(xyz, new List<Pole2D>
					{
						pole2D
					});
					dictionary.Add(xyz, value2);
				}
				else
				{
					dictionary[xyz].AddPole(pole2D, false);
				}
			}
			foreach (KeyValuePair<XYZ, Node2D> keyValuePair in dictionary)
			{
				if (searchType == Node2D.SearchType._Anticlockwise)
				{
					keyValuePair.Value.AnticlockwiseSortElements();
				}
				else
				{
					keyValuePair.Value.ClockwiseSortElements();
				}
				theNodes.Add(keyValuePair.Value);
			}
		}

		private void MergeEdge(ref List<Node2D> theNodes)
		{
			Node2D leftBottomNode = this.GetLeftBottomNode(theNodes);
			for (int i = 0; i < theNodes.Count; i++)
			{
				Node2D node2D = theNodes[i];
				if (node2D.PoleList.Count == 2 && !Geometry.IsEqual(leftBottomNode.Position, node2D.Position))
				{
					Pole2D newPole = null;
					if (Pole2D.MergeTwoPole(node2D.PoleList[0], node2D.PoleList[1], ref newPole, false))
					{
						this.UpdateLinkedPole(node2D, node2D.PoleList[0], theNodes, newPole);
						this.UpdateLinkedPole(node2D, node2D.PoleList[1], theNodes, newPole);
						theNodes.RemoveAt(i);
						i--;
					}
				}
			}
		}

		private void UpdateLinkedPole(Node2D curNode, Pole2D curPole, List<Node2D> theNodes, Pole2D newPole)
		{
			XYZ pos = curPole.CurEndPoint(curNode.Position);
			Node2D node2D = this.FindNodeByPos(theNodes, pos);
			int num = node2D.FindLinkPoleWithOtherNode(curNode.Position, Node2D.SearchType._Anticlockwise);
			if (-1 != num)
			{
				node2D.PoleList[num] = newPole;
			}
		}

		private Node2D FindNodeByPos(List<Node2D> theNodes, XYZ pos)
		{
			foreach (Node2D node2D in theNodes)
			{
				if (Geometry.IsEqual(pos.DistanceTo(node2D.Position), 0.0, 0.0001))
				{
					return node2D;
				}
			}
			return null;
		}

		private bool CompareXYLess(XYZ lhv, XYZ rhv)
		{
			if (Geometry.IsEqual(lhv.Y, rhv.Y))
			{
				if (Geometry.LessThan(lhv.X, rhv.X))
				{
					return true;
				}
			}
			else if (Geometry.LessThan(lhv.Y, rhv.Y))
			{
				return true;
			}
			return false;
		}

		private Node2D GetLeftBottomNode(List<Node2D> theNodes)
		{
			XYZ xyz = theNodes[0].Position;
			Node2D result = null;
			foreach (Node2D node2D in theNodes)
			{
				XYZ position = node2D.Position;
				if (Geometry.IsEqual(position.DistanceTo(xyz), 0.0, 0.0001))
				{
					result = node2D;
				}
				else if (this.CompareXYLess(position, xyz))
				{
					xyz = position;
					result = node2D;
				}
			}
			return result;
		}

		private void GetAllPoleList(List<Node2D> theNodes, ref List<Pole2D> analyzedPoleList)
		{
			foreach (Node2D node2D in theNodes)
			{
				using (List<Pole2D>.Enumerator enumerator2 = node2D.PoleList.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						Pole2D pole = enumerator2.Current;
						if (analyzedPoleList.FindIndex((Pole2D item) => item.SameAs(pole)) == -1)
						{
							analyzedPoleList.Add(pole);
						}
					}
				}
			}
		}

		private void MergeOverlapWalls(ref List<Pole2D> analyzedPoleList)
		{
			for (int i = 0; i < analyzedPoleList.Count; i++)
			{
				bool flag = false;
				for (int j = i + 1; j < analyzedPoleList.Count; j++)
				{
					if (this.MergeOverlapTwoWall(analyzedPoleList[i], analyzedPoleList[j], ref analyzedPoleList))
					{
						flag = true;
						analyzedPoleList.RemoveAt(j);
						break;
					}
				}
				if (flag)
				{
					analyzedPoleList.RemoveAt(i);
					i--;
				}
			}
		}

		private bool MergeOverlapTwoWall(Pole2D pole1, Pole2D pole2, ref List<Pole2D> mergedOverlapWalls)
		{
			if (Geometry.IsEqual(pole2.Elevation, pole1.Elevation))
			{
				return false;
			}
			Pole2D pole2D = pole1;
			Pole2D pole2D2 = pole2;
			if (Geometry.GreaterThan(pole2.Elevation, pole1.Elevation))
			{
				pole2D = pole2;
				pole2D2 = pole1;
			}
			if (pole2D.IsArcPole != pole2D2.IsArcPole)
			{
				return false;
			}
			if (pole2D.IsArcPole)
			{
				return this.MergeOverlapTwoArcWall(pole2D, pole2D2, ref mergedOverlapWalls);
			}
			return this.MergeOverlapTwoLineWall(pole2D, pole2D2, ref mergedOverlapWalls);
		}

		private bool MergeOverlapTwoArcWall(Pole2D poleTop, Pole2D poleBottom, ref List<Pole2D> mergedOverlapWalls)
		{
			if (!Geometry.IsEqual(poleTop.Center, poleBottom.Center, 0.0001))
			{
				return false;
			}
			if (Geometry.GreaterThan(Math.Abs(poleTop.Radius - poleBottom.Radius), (poleTop.Width + poleBottom.Width) / 2.0))
			{
				return false;
			}
			XYZ ptS = poleTop.StartPoint;
			XYZ ptE = poleTop.EndPoint;
			if (Geometry.LessThan(poleTop.Bulge, 0.0))
			{
				ptS = poleTop.EndPoint;
				ptE = poleTop.StartPoint;
			}
			XYZ xyz = poleBottom.StartPoint;
			XYZ xyz2 = poleBottom.EndPoint;
			if (Geometry.LessThan(poleBottom.Bulge, 0.0))
			{
				xyz = poleBottom.EndPoint;
				xyz2 = poleBottom.StartPoint;
			}
			xyz = poleTop.Center + (xyz - poleTop.Center).Normalize() * poleBottom.Radius;
			xyz2 = poleTop.Center + (xyz2 - poleTop.Center).Normalize() * poleBottom.Radius;
			XYZ ptOverlapStart = null;
			XYZ ptOverlapEnd = null;
			Geometry.OverlapType overlapType = Geometry.IsOverlapOfTwoArc(ptS, ptE, poleTop.Center, xyz, xyz2, poleTop.Center, ref ptOverlapStart, ref ptOverlapEnd, 1E-09);
			this.CorrectOverlapType(poleTop, poleBottom, ptOverlapStart, ptOverlapEnd, ref overlapType);
			if (overlapType == Geometry.OverlapType._OT_Linked || overlapType == Geometry.OverlapType._OT_None)
			{
				return false;
			}
			if (overlapType == Geometry.OverlapType._OT_Equality || overlapType == Geometry.OverlapType._OT_FirstIncludeSecond)
			{
				mergedOverlapWalls.Add(poleTop);
			}
			else if (overlapType == Geometry.OverlapType._OT_SecondIncludeFirst)
			{
				this.OperateSecondIncludeFirstWithArc(poleTop, poleBottom, ref mergedOverlapWalls);
			}
			else if (overlapType == Geometry.OverlapType._OT_Part)
			{
				this.OperatePartWithArc(poleTop, poleBottom, ref mergedOverlapWalls);
			}
			return true;
		}

		private bool MergeOverlapTwoLineWall(Pole2D poleTop, Pole2D poleBottom, ref List<Pole2D> mergedOverlapWalls)
		{
			if (!Geometry.IsParallel(poleTop.Normal, poleBottom.Normal, 1E-09))
			{
				return false;
			}
			XYZ xyz = Geometry.CalculateFootPoint(poleTop.StartPoint, poleTop.EndPoint, poleBottom.StartPoint);
			if (Geometry.GreaterThan(xyz.DistanceTo(poleBottom.StartPoint), (poleTop.Width + poleBottom.Width) / 2.0))
			{
				return false;
			}
			XYZ ptE = Geometry.CalculateFootPoint(poleTop.StartPoint, poleTop.EndPoint, poleBottom.EndPoint);
			XYZ ptOverlapStart = null;
			XYZ ptOverlapEnd = null;
			Geometry.OverlapType overlapType = Geometry.IsOverlapOfTwoLine(poleTop.StartPoint, poleTop.EndPoint, xyz, ptE, ref ptOverlapStart, ref ptOverlapEnd, true, 1E-09);
			this.CorrectOverlapType(poleTop, poleBottom, ptOverlapStart, ptOverlapEnd, ref overlapType);
			if (overlapType == Geometry.OverlapType._OT_Linked || overlapType == Geometry.OverlapType._OT_None)
			{
				return false;
			}
			if (overlapType == Geometry.OverlapType._OT_Equality || overlapType == Geometry.OverlapType._OT_FirstIncludeSecond)
			{
				mergedOverlapWalls.Add(poleTop);
			}
			else if (overlapType == Geometry.OverlapType._OT_SecondIncludeFirst)
			{
				this.OperateSecondIncludeFirstWithLine(poleTop, poleBottom, ref mergedOverlapWalls);
			}
			else if (overlapType == Geometry.OverlapType._OT_Part)
			{
				this.OperatePartWithLine(poleTop, poleBottom, ref mergedOverlapWalls);
			}
			return true;
		}

		private void CorrectOverlapType(Pole2D poleTop, Pole2D poleBottom, XYZ ptOverlapStart, XYZ ptOverlapEnd, ref Geometry.OverlapType overlayType)
		{
			if (overlayType == Geometry.OverlapType._OT_FirstIncludeSecond || overlayType == Geometry.OverlapType._OT_SecondIncludeFirst)
			{
				double val = Math.Abs(poleTop.Length() - poleBottom.Length());
				double val2 = (poleTop.Width + poleBottom.Width) / 2.0;
				if (Geometry.Lessthan_Or_Equal(val, val2))
				{
					overlayType = Geometry.OverlapType._OT_Equality;
					return;
				}
			}
			else if (overlayType == Geometry.OverlapType._OT_Part)
			{
				double val3 = (poleTop.Width + poleBottom.Width) / 2.0;
				double num = ptOverlapEnd.DistanceTo(ptOverlapStart);
				double val4 = Math.Abs(poleTop.Length() - num);
				double val5 = Math.Abs(poleBottom.Length() - num);
				if (Geometry.Lessthan_Or_Equal(val4, val3) && Geometry.Lessthan_Or_Equal(val5, val3))
				{
					overlayType = Geometry.OverlapType._OT_Equality;
					return;
				}
				if (Geometry.Lessthan_Or_Equal(val4, val3))
				{
					overlayType = Geometry.OverlapType._OT_SecondIncludeFirst;
					return;
				}
				if (Geometry.Lessthan_Or_Equal(val5, val3))
				{
					overlayType = Geometry.OverlapType._OT_FirstIncludeSecond;
				}
			}
		}

		private void OperateSecondIncludeFirstWithArc(Pole2D top, Pole2D bottom, ref List<Pole2D> mergedOverlapWalls)
		{
			mergedOverlapWalls.Add(top);
			XYZ xyz = top.Center + (top.StartPoint - top.Center).Normalize() * bottom.Radius;
			XYZ xyz2 = top.Center + (top.EndPoint - top.Center).Normalize() * bottom.Radius;
			if (Geometry.IsEqual(top.Normal, bottom.Normal))
			{
				Pole2D item = Pole2D.NewStructure2DPole(bottom.ComponentId, bottom.StartPoint, xyz, bottom.Center, bottom.Normal, bottom.Elevation, bottom.PositiveHalfWidth, bottom.NegativeHalfWidth);
				mergedOverlapWalls.Add(item);
				Pole2D item2 = Pole2D.NewStructure2DPole(bottom.ComponentId, xyz2, bottom.EndPoint, bottom.Center, bottom.Normal, bottom.Elevation, bottom.PositiveHalfWidth, bottom.NegativeHalfWidth);
				mergedOverlapWalls.Add(item2);
				return;
			}
			Pole2D item3 = Pole2D.NewStructure2DPole(bottom.ComponentId, bottom.StartPoint, xyz2, bottom.Center, bottom.Normal, bottom.Elevation, bottom.PositiveHalfWidth, bottom.NegativeHalfWidth);
			mergedOverlapWalls.Add(item3);
			Pole2D item4 = Pole2D.NewStructure2DPole(bottom.ComponentId, xyz, bottom.EndPoint, bottom.Center, bottom.Normal, bottom.Elevation, bottom.PositiveHalfWidth, bottom.NegativeHalfWidth);
			mergedOverlapWalls.Add(item4);
		}

		private void OperatePartWithArc(Pole2D top, Pole2D bottom, ref List<Pole2D> mergedOverlapWalls)
		{
			mergedOverlapWalls.Add(top);
			XYZ ptEnd = top.Center + (top.StartPoint - top.Center).Normalize() * bottom.Radius;
			XYZ ptEnd2 = top.Center + (top.EndPoint - top.Center).Normalize() * bottom.Radius;
			XYZ xyz = Geometry.CalculatMidPoint(top.StartPoint, top.EndPoint);
			double val = bottom.StartPoint.DistanceTo(xyz);
			double val2 = bottom.EndPoint.DistanceTo(xyz);
			if (Geometry.GreaterThan(val, val2))
			{
				double val3 = top.StartPoint.DistanceTo(bottom.StartPoint);
				double val4 = top.EndPoint.DistanceTo(bottom.StartPoint);
				if (Geometry.GreaterThan(val3, val4))
				{
					Pole2D item = Pole2D.NewStructure2DPole(bottom.ComponentId, bottom.StartPoint, ptEnd2, bottom.Center, bottom.Normal, bottom.Elevation, bottom.PositiveHalfWidth, bottom.NegativeHalfWidth);
					mergedOverlapWalls.Add(item);
					return;
				}
				Pole2D item2 = Pole2D.NewStructure2DPole(bottom.ComponentId, bottom.StartPoint, ptEnd, bottom.Center, bottom.Normal, bottom.Elevation, bottom.PositiveHalfWidth, bottom.NegativeHalfWidth);
				mergedOverlapWalls.Add(item2);
				return;
			}
			else
			{
				double val5 = top.StartPoint.DistanceTo(bottom.EndPoint);
				double val6 = top.EndPoint.DistanceTo(bottom.EndPoint);
				if (Geometry.GreaterThan(val5, val6))
				{
					Pole2D item3 = Pole2D.NewStructure2DPole(bottom.ComponentId, bottom.EndPoint, ptEnd2, bottom.Center, bottom.Normal, bottom.Elevation, bottom.PositiveHalfWidth, bottom.NegativeHalfWidth);
					mergedOverlapWalls.Add(item3);
					return;
				}
				Pole2D item4 = Pole2D.NewStructure2DPole(bottom.ComponentId, bottom.EndPoint, ptEnd, bottom.Center, bottom.Normal, bottom.Elevation, bottom.PositiveHalfWidth, bottom.NegativeHalfWidth);
				mergedOverlapWalls.Add(item4);
				return;
			}
		}

		private void OperateSecondIncludeFirstWithLine(Pole2D top, Pole2D bottom, ref List<Pole2D> mergedOverlapWalls)
		{
			mergedOverlapWalls.Add(top);
			XYZ xyz = Geometry.CalculateFootPoint(bottom.StartPoint, bottom.EndPoint, top.StartPoint);
			XYZ xyz2 = Geometry.CalculateFootPoint(bottom.StartPoint, bottom.EndPoint, top.EndPoint);
			if (Geometry.IsEqual(top.Normal, bottom.Normal))
			{
				Pole2D item = Pole2D.NewStructure2DPole(bottom.ComponentId, bottom.StartPoint, xyz, bottom.Elevation, bottom.PositiveHalfWidth, bottom.NegativeHalfWidth);
				mergedOverlapWalls.Add(item);
				Pole2D item2 = Pole2D.NewStructure2DPole(bottom.ComponentId, xyz2, bottom.EndPoint, bottom.Elevation, bottom.PositiveHalfWidth, bottom.NegativeHalfWidth);
				mergedOverlapWalls.Add(item2);
				return;
			}
			Pole2D item3 = Pole2D.NewStructure2DPole(bottom.ComponentId, bottom.StartPoint, xyz2, bottom.Elevation, bottom.PositiveHalfWidth, bottom.NegativeHalfWidth);
			mergedOverlapWalls.Add(item3);
			Pole2D item4 = Pole2D.NewStructure2DPole(bottom.ComponentId, xyz, bottom.EndPoint, bottom.Elevation, bottom.PositiveHalfWidth, bottom.NegativeHalfWidth);
			mergedOverlapWalls.Add(item4);
		}

		private void OperatePartWithLine(Pole2D top, Pole2D bottom, ref List<Pole2D> mergedOverlapWalls)
		{
			mergedOverlapWalls.Add(top);
			XYZ ptEnd = Geometry.CalculateFootPoint(bottom.StartPoint, bottom.EndPoint, top.StartPoint);
			XYZ ptEnd2 = Geometry.CalculateFootPoint(bottom.StartPoint, bottom.EndPoint, top.EndPoint);
			XYZ xyz = Geometry.CalculatMidPoint(top.StartPoint, top.EndPoint);
			double val = bottom.StartPoint.DistanceTo(xyz);
			double val2 = bottom.EndPoint.DistanceTo(xyz);
			if (Geometry.GreaterThan(val, val2))
			{
				double val3 = top.StartPoint.DistanceTo(bottom.StartPoint);
				double val4 = top.EndPoint.DistanceTo(bottom.StartPoint);
				if (Geometry.GreaterThan(val3, val4))
				{
					Pole2D item = Pole2D.NewStructure2DPole(bottom.ComponentId, bottom.StartPoint, ptEnd2, bottom.Elevation, bottom.PositiveHalfWidth, bottom.NegativeHalfWidth);
					mergedOverlapWalls.Add(item);
					return;
				}
				Pole2D item2 = Pole2D.NewStructure2DPole(bottom.ComponentId, bottom.StartPoint, ptEnd, bottom.Elevation, bottom.PositiveHalfWidth, bottom.NegativeHalfWidth);
				mergedOverlapWalls.Add(item2);
				return;
			}
			else
			{
				double val5 = top.StartPoint.DistanceTo(bottom.EndPoint);
				double val6 = top.EndPoint.DistanceTo(bottom.EndPoint);
				if (Geometry.GreaterThan(val5, val6))
				{
					Pole2D item3 = Pole2D.NewStructure2DPole(bottom.ComponentId, bottom.EndPoint, ptEnd2, bottom.Elevation, bottom.PositiveHalfWidth, bottom.NegativeHalfWidth);
					mergedOverlapWalls.Add(item3);
					return;
				}
				Pole2D item4 = Pole2D.NewStructure2DPole(bottom.ComponentId, bottom.EndPoint, ptEnd, bottom.Elevation, bottom.PositiveHalfWidth, bottom.NegativeHalfWidth);
				mergedOverlapWalls.Add(item4);
				return;
			}
		}

		private ExternalCommandData m_Revit;

		private class PointComparer : IEqualityComparer<XYZ>
		{
			public bool Equals(XYZ p, XYZ q)
			{
				return Geometry.IsEqual(p.DistanceTo(q), 0.0, 0.0001);
			}

			public int GetHashCode(XYZ p)
			{
				string text = p.X.ToString("#0.0");
				string text2 = p.Y.ToString("#0.0");
				string text3 = p.Z.ToString("#0.0");
				return string.Concat(new string[]
				{
					"(",
					text,
					",",
					text2,
					",",
					text3,
					")"
				}).GetHashCode();
			}
		}
	}
}
